<?php

namespace app\api\controller;

use app\api\logic\UserFootprintLogic;
use app\basic\ApiController;
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("足迹接口")
 */
class FootprintController extends ApiController
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->logic = new UserFootprintLogic();
        parent::__construct();
    }

    /**
     * @Apidoc\Title("获取足迹统计")
     * @Apidoc\Method("POST")
     * @Apidoc\Returned("data", type="object", desc="返回数据", children={
     *     @Apidoc\Returned("today_count", type="int", desc="今日浏览"),
     *     @Apidoc\Returned("total_count", type="int", desc="总浏览")
     * })
     */
    public function getCount()
    {
        $userId = $this->auth->id;
        $data = $this->logic->getFootprintCount($userId);
        return $this->success($data);
    }

    /**
     * @Apidoc\Title("获取足迹列表")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("page", type="int", default="1", desc="页码")
     * @Apidoc\Param("limit", type="int", default="10", desc="每页数量")
     * @Apidoc\Returned("data", type="object", desc="返回数据", children={
     *     @Apidoc\Returned("list", type="array", desc="足迹列表", children={
     *         @Apidoc\Returned("id", type="int", desc="足迹ID"),
     *         @Apidoc\Returned("article_id", type="int", desc="文章ID"),
     *         @Apidoc\Returned("title", type="string", desc="标题"),
     *         @Apidoc\Returned("view_count", type="int", desc="浏览次数"),
     *         @Apidoc\Returned("create_time", type="string", desc="浏览时间"),
     *     }),
     *     @Apidoc\Returned("total", type="int", desc="总记录数"),
     *     @Apidoc\Returned("page", type="int", desc="当前页码"),
     *     @Apidoc\Returned("limit", type="int", desc="每页数量")
     * })
     */
    public function getList()
    {
        $page = $this->request->post('page', 1);
        $limit = $this->request->post('limit', 10);
        
        $list = $this->logic->getFootprintList($this->auth->id, $page, $limit);
        return $this->success($list);
    }

    /**
     * @Apidoc\Title("批量删除足迹")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("ids", type="string", require=true, desc="足迹ID，多个以逗号分隔，如：8,9")
     */
    public function batchDelete()
    {
        $ids = $this->request->post('ids', '');
        // 将逗号分隔的字符串转换为数组，并过滤掉非法值
        $idArray = array_filter(explode(',', $ids), function($id) {
            return is_numeric($id) && $id > 0;
        });
        
        if (empty($idArray)) {
            return $this->fail('参数错误');
        }
        
        $result = $this->logic->deleteFootprint($this->auth->id, $idArray);
        return $result ? $this->success('删除成功') : $this->fail('删除失败');
    }

} 